<template>
  <div>
    <el-dialog title="编辑信息" :visible.sync="editVisible" width="40%" :show-close="false" :close-on-click-modal="false">
      <div v-for="(item, index) in columnTitle" :key="index">
        <el-row v-if="item.label != '数据来源'" :gutter="10" style="margin-bottom: 10px">
          <el-col :span="8"> {{ item.label }}: </el-col>
          <el-col :span="11"> <el-input v-model="editRow[columnTitle[index].prop]"></el-input> </el-col>
        </el-row>
        <el-row v-else :gutter="10" style="margin-bottom: 10px">
          <el-col :span="8"> {{ item.label }}: </el-col>
          <el-col :span="11">
            <a-upload name="file" :fileList="fileList" :data="{ id: rowId, type: uploadType }" :action="uploadUrl" :multiple="true" @change="handleChange">
              <!-- <el-button size="mini">上传数据</el-button> -->
            </a-upload>
          </el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" style="margin-right: 20px" @click="confirmEdit()">确认</el-button>
        <el-button type="danger" size="small" style="margin-right: 20px" @click="closeEdit()">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from 'element-ui'
export default {
  props: {
    importDataType: '' //判断是软磁、永磁、导电
  },
  data() {
    return {
      editVisible: false,
      editRow: {},
      columnTitle: [],
      uploadUrl: 'http://59.172.104.174:30669/attachmentUpload/uploadAttachmentFile',
      rowId: '',
      fileList: [],
      fileListName: '',
      uploadType: ''
    }
  },
  methods: {
    handleChange(info) {
      if (info.file.status == 'removed') {
        this.axios({
          baseURL: 'http://59.172.104.174:30669',
          url: '/attachmentUpload/deleteAttachmentFile',
          method: 'GET',
          params: {
            filename: this.fileListName,
            id: this.rowId,
            type: this.uploadType
          }
        }).then(res => {
          this.fileList = info.fileList
          Message({
            message: '删除成功！',
            type: 'success',
            center: true,
            customClass: 'mzindex'
          })
        })
      } else if (info.file.status == 'uploading') {
        this.fileList = []
        this.fileList = info.fileList
      }
    },
    closeEdit() {
      this.$emit('editSuccess')
      this.editVisible = false
    },
    confirmEdit() {
      if (this.importDataType == '软磁性能') {
        this.axios({
          baseURL: 'http://59.172.104.174:30669',
          url: '/checkTable/editProductParameter',
          method: 'POST',
          data: this.editRow
        }).then(res => {
          if (res.data.code == 200) {
            Message({
              message: res.data.message,
              type: 'success',
              center: true,
              customClass: 'mzindex'
            })
            this.$emit('editSuccess')
            this.editVisible = false
          } else {
            Message({
              message: res.data.message,
              type: 'error',
              center: true,
              customClass: 'mzindex'
            })
            this.editVisible = false
          }
        })
      } else if (this.importDataType == '永磁性能') {
        this.axios({
          baseURL: 'http://59.172.104.174:30669',
          url: '/checkTable/editPerParameter',
          method: 'POST',
          data: this.editRow
        }).then(res => {
          if (res.data.code == 200) {
            Message({
              message: res.data.message,
              type: 'success',
              center: true,
              customClass: 'mzindex'
            })
            this.$emit('editSuccess')
            this.editVisible = false
          } else {
            Message({
              message: res.data.message,
              type: 'error',
              center: true,
              customClass: 'mzindex'
            })
            this.editVisible = false
          }
        })
      } else if (this.importDataType == '导电性能') {
        this.axios({
          baseURL: 'http://59.172.104.174:30669',
          url: '/checkTable/editEleParameter',
          method: 'POST',
          data: this.editRow
        }).then(res => {
          if (res.data.code == 200) {
            Message({
              message: res.data.message,
              type: 'success',
              center: true,
              customClass: 'mzindex'
            })
            this.$emit('editSuccess')
            this.editVisible = false
          } else {
            Message({
              message: res.data.message,
              type: 'error',
              center: true,
              customClass: 'mzindex'
            })
            this.editVisible = false
          }
        })
      }
    },
    getEditRowAndColumn(row, columnTitle, editVisible) {
      this.editRow = JSON.parse(JSON.stringify(row)) //避免浅拷贝，修改父组件传过来的值的时候，是父组件的也改变了
      this.columnTitle = JSON.parse(JSON.stringify(columnTitle)) //避免浅拷贝，修改父组件传过来的值的时候，是父组件的也改变了
      this.editVisible = editVisible
      if (this.importDataType == '软磁性能') {
        this.uploadType = '软磁样品基本参数'
      } else if (this.importDataType == '永磁性能') {
        this.uploadType = '永磁样品基本参数'
      } else if (this.importDataType == '导电性能') {
        this.uploadType = '导电样品基本参数'
      }
      this.rowId = row.id
      this.fileListName = ''
      this.fileListName = this.editRow[this.columnTitle[this.columnTitle.length - 1].prop].toString()
      this.fileList = []
      if (this.fileListName != '') {
        this.fileList.push({
          uid: '1',
          name: this.fileListName,
          status: 'done'
        })
      }
    }
  }
}
</script>

<style></style>
